package com.game.majiang.vo;


import java.sql.SQLException;
import java.sql.SQLOutput;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.*;

import javax.sql.rowset.CachedRowSet;

import com.alibaba.fastjson.JSON;
import com.game.majiang.config.LogManager;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;

import lombok.Data;

@Data
public class UserDBVo {


  public UserDBVo() {

  }


  public UserDBVo(int id) {
    long now = System.currentTimeMillis();
     // long aa = new Date().getTime();
     // String cmdText = "select id,account,nickname,gold,head,headtype,sex,logintime,autograph,integral,nameupdate,"
     //     + "zongjucount,zongshoucount,zongshouwincount,zupaicount,zupaiwincount,zongwinjucount,zonglosejucount, "
     //     + " from users where id=?";
    List<String> fields = Arrays.asList("id", "account", "nickname", "gold", "head", "headtype", "sex", "logintime", "autograph",
        "integral", "nameupdate", "zongjucount", "zongshoucount", "zongshouwincount", "zupaicount", "zupaiwincount", "zongwinjucount",
        "zonglosejucount", "is_robot");
    StringBuilder cmd = new StringBuilder();
    cmd.append("SELECT \n");
		cmd.append("\t`").append(String.join("`,\n\t`", fields)).append("`\n");
    cmd.append("FROM `users` \n");
    cmd.append("WHERE `id` = ? \n");
    String cmdText = cmd.toString();
    Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
    CachedRowSet rs = SqlHelperwrite.getResultSet(cmdText, p1);
    try {
      if (rs.next()) {
        this.Id = rs.getInt(1);
        this.Account = rs.getString(2);
        LogManager.getInstance().Log("Account:" + this.Account);
        this.NickName = rs.getString(3);
        this.Gold = rs.getInt(4);
        this.Head = rs.getString(5);
        this.HeadType = rs.getByte(6);
        this.Sex = rs.getByte(7);
        this.LastTime = rs.getString(8);
        LogManager.getInstance().Log("用戶db_LastTime:" + this.LastTime);
        this.autograph = rs.getString("autograph");
        this.integral = rs.getInt("integral");
        this.nameupdate = rs.getInt("nameupdate");
        this.zongjucount = rs.getInt("zongjucount");
        this.zongshoucount = rs.getInt("zongshoucount");
        this.zongshouwincount = rs.getInt("zongshouwincount");
        this.zupaicount = rs.getInt("zupaicount");
        this.zupaiwincount = rs.getInt("zupaiwincount");
        this.zongwinjucount = rs.getInt("zongwinjucount");
        this.zonglosejucount = rs.getInt("zonglosejucount");

      }
    } catch (SQLException e) {
      e.printStackTrace();
      LogManager.getInstance().Error("Sql:" + cmdText + "错误！");
    }
    //		long aa2 = new Date().getTime();
    //LogManager.getInstance().Error("!!!!!!!!!!"+(aa2 - aa));
  }

  public UserDBVo(int id, RobotVo rv) {
    String cmdText = "select id,account,nickname,gold,head,headtype,sex,logintime,everydaysign,autograph,integral,task,prop,nameupdate from robot where id=? ";
    Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
    CachedRowSet rs = SqlHelper.getResultSet(cmdText, p1);
    try {
      if (rs.next()) {
        this.Id = rs.getInt(1);
        this.Account = rs.getString(2);
        this.NickName = rs.getString(3);
        this.Gold = rs.getInt(4);
        this.Head = rs.getString(5);
        this.HeadType = rs.getByte(6);
        this.Sex = rs.getByte(7);
        this.LastTime = rs.getString(8);
        //				this.allwin = rs.getInt(10);
        //				this.alllose = rs.getInt(11);
        //				this.allbet = rs.getInt(12);
        //				this.owncode = rs.getString(13);
        //				this.otherscode = rs.getString(14);
        //				this.card = rs.getInt(15);
        //				this.huoqucard = rs.getInt(16);
        this.autograph = rs.getString(10);
        //				this.warehouse = rs.getInt(18);
        this.integral = rs.getInt(11);
        this.nameupdate = rs.getInt(12);

        this.isRobot = rs.getInt(13);
      }
    } catch (SQLException e) {
      e.printStackTrace();
      LogManager.getInstance().Error("Sql:" + cmdText + "错误！");
    }

  }

  private int Id;

  private String Account;

  private String NickName;

  private int Gold;

  private String Head;

  private byte HeadType;

  private byte Sex;
  private String LastTime;//上次退出时间   登录时候获取
  private String LoginTime;//这次登陆时间  登录时赋值


  private String autograph;

  private int diamonds;//钻石
  private int integral;//积分
  private int nameupdate;//昵称修改免费次数


  private String longitude;//经度
  private String latitude;//纬度
  private int clubid = 0;
  private int limitipgps = 0;
  private String createlongitude = "";//创建时候的经度
  private String createlatitude = "";//创建时候的纬度
  private ArrayList<String> listWarpAndWefts = new ArrayList<String>();//最近几次登录gps longitude&latitude

  private int zongjucount = 0;
  private int zongshoucount = 0;
  private int zongshouwincount = 0;
  private int zupaicount = 0;
  private int zupaiwincount = 0;


  private int zongwinjucount = 0;//总赢局数
  private int zonglosejucount = 0;//总输局数

  private int isRobot = 0; // 是否机器人


  /**
   * 保存用户数据
   *
   * @param ip
   * @return
   */
  public boolean Save(String ip) {
    String cmdText = "";

    cmdText = "update users set gold=?,ip=?,logintime=?,"
        + "head=? ,diamonds=?,integral=?, zongjucount = ?,"
        + "zongshoucount=? ,zongshouwincount = ?, zupaicount = ?, "
        + "zupaiwincount = ?,zongwinjucount=?,zonglosejucount=?"

        + " where Id=?";

    Parameter pid = new Parameter(Types.INTEGER, Id, ParameterType.Input);
    Parameter p1 = new Parameter(Types.INTEGER, Gold, ParameterType.Input);
    Parameter p2 = new Parameter(Types.VARCHAR, ip, ParameterType.Input);
    Parameter p3 = new Parameter(Types.TIMESTAMP, new Timestamp(new Date().getTime()), ParameterType.Input);
    Parameter p4 = new Parameter(Types.VARCHAR, Head, ParameterType.Input);
    Parameter p5 = new Parameter(Types.INTEGER, diamonds, ParameterType.Input);
    Parameter p6 = new Parameter(Types.INTEGER, integral, ParameterType.Input);
    Parameter p7 = new Parameter(Types.INTEGER, this.zongjucount, ParameterType.Input);
    Parameter p8 = new Parameter(Types.INTEGER, this.zongshoucount, ParameterType.Input);
    Parameter p9 = new Parameter(Types.INTEGER, this.zongshouwincount, ParameterType.Input);
    Parameter p10 = new Parameter(Types.INTEGER, this.zupaicount, ParameterType.Input);
    Parameter p11 = new Parameter(Types.INTEGER, this.zupaiwincount, ParameterType.Input);
    Parameter p12 = new Parameter(Types.INTEGER, this.zongwinjucount, ParameterType.Input);
    Parameter p13 = new Parameter(Types.INTEGER, this.zonglosejucount, ParameterType.Input);

    if (SqlHelperwrite.execute(cmdText, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, pid) > 0) {
      return true;
    } else {
      return false;
    }

  }


  /**
   * 保存用户信息
   *
   * @return
   */
  public boolean Save() {
    String cmdText = "update users set gold=?,diamonds=?,integral=?"
        + " where Id = ?";
    Parameter pid = new Parameter(Types.INTEGER, Id, ParameterType.Input);
    Parameter p1 = new Parameter(Types.INTEGER, Gold, ParameterType.Input);
    Parameter p2 = new Parameter(Types.INTEGER, diamonds, ParameterType.Input);
    Parameter p3 = new Parameter(Types.INTEGER, integral, ParameterType.Input);
    if (SqlHelperwrite.execute(cmdText, p1, p2, p3, pid) > 0) {
      return true;
    } else {
      return false;
    }


  }

  public boolean Savenomajin() {
    String cmdText = "update users set gold=?,diamonds=?,integral=?"
        //				+ ",prop=?,task=?,charm=?,"
        //				+" victorycount=?,warehouseDiamonds=?,warehouseIntegral=?,nickname=?,"
        //				+ "zongjushu=?,zongshoushu=?,junchangzhanji=?,junchangdairufen=?,"
        //				+ "zongshengjushu=?,zongkaipaishu=?,zongshengshoushu=?,zongmangguoshu=?,zongyuanshibobo=?,"
        //				+ "chexuanzongjushu=?,chexuanzongshouju=?,maguzongjushu=?,maguzongshoushu=?,chemashoujustr=?,"
        //				+ "zongfushoushu=?"
        + " where Id = ?";
    Parameter pid = new Parameter(Types.INTEGER, Id, ParameterType.Input);
    Parameter p1 = new Parameter(Types.INTEGER, Gold, ParameterType.Input);
    Parameter p2 = new Parameter(Types.INTEGER, diamonds, ParameterType.Input);
    Parameter p3 = new Parameter(Types.INTEGER, integral, ParameterType.Input);
    //		Parameter p4 = new Parameter(Types.INTEGER,allbet, ParameterType.Input);
    //		Parameter p5 = new Parameter(Types.INTEGER,card, ParameterType.Input);
    //		Parameter p6 = new Parameter(Types.INTEGER, warehouse, ParameterType.Input);

    //		JSONArray jsonArray = JSONArray.fromObject(prop);
    //		Parameter p8 = new Parameter(Types.VARCHAR, jsonArray.toString(), ParameterType.Input);
    //
    //		Parameter p9 = new Parameter(Types.VARCHAR, "", ParameterType.Input);
    //		Parameter p10 = new Parameter(Types.INTEGER,charmcount, ParameterType.Input);
    //		Parameter p11 = new Parameter(Types.INTEGER,victorycount, ParameterType.Input);
    //		Parameter p12 = new Parameter(Types.INTEGER, warehouseDiamonds, ParameterType.Input);
    //		Parameter p13 = new Parameter(Types.INTEGER, warehouseIntegral, ParameterType.Input);
    //		Parameter p19 = new Parameter(Types.VARCHAR, NickName, ParameterType.Input);
    //		Parameter p22 = new Parameter(Types.INTEGER,zongjushu, ParameterType.Input);
    //		Parameter p23 = new Parameter(Types.INTEGER,zongshoushu, ParameterType.Input);
    //		Parameter p24 = new Parameter(Types.INTEGER,junchangzhanji, ParameterType.Input);
    //		Parameter p25 = new Parameter(Types.INTEGER,junchangdairufen, ParameterType.Input);
    //		Parameter p28 = new Parameter(Types.INTEGER,zongshengjushu, ParameterType.Input);
    //		Parameter p29 = new Parameter(Types.INTEGER,zongkaipaishu, ParameterType.Input);
    //		Parameter p30 = new Parameter(Types.INTEGER,zongshengshoushu, ParameterType.Input);
    //		Parameter p31 = new Parameter(Types.INTEGER,zongmangguoshu, ParameterType.Input);
    //		Parameter p32 = new Parameter(Types.INTEGER,zongyuanshibobo, ParameterType.Input);
    //		Parameter p38 = new Parameter(Types.INTEGER, chexuanzongjushu, ParameterType.Input);
    //		Parameter p39 = new Parameter(Types.INTEGER, chexuanzongshouju, ParameterType.Input);
    //		Parameter p40 = new Parameter(Types.INTEGER, maguzongjushu, ParameterType.Input);
    //		Parameter p41 = new Parameter(Types.INTEGER, maguzongshoushu, ParameterType.Input);
    //
    //		String chemashoujustr=",0,0,0,0,0,0,0,0,0,0,0,0";
    //		StringBuilder shouju = new StringBuilder();
    //		shouju.append(","+chexuandatingjushu);
    //		shouju.append(","+chexuanhaoyoujushu);
    //		shouju.append(","+chexuanlianmengjushu);
    //		shouju.append(","+chexuandatingshouju);
    //		shouju.append(","+chexuanhaoyoushouju);
    //		shouju.append(","+chexuanlianmengshouju);
    //		shouju.append(","+magudatingjushu);
    //		shouju.append(","+maguhaoyoujushu);
    //		shouju.append(","+magulianmengjushu);
    //		shouju.append(","+magudatingshouju);
    //		shouju.append(","+maguhaoyoushouju);
    //		shouju.append(","+magulianmengshouju);
    //		chemashoujustr=shouju.toString();
    //		Parameter p42 = new Parameter(Types.VARCHAR, chemashoujustr, ParameterType.Input);
    //
    //		Parameter p61 = new Parameter(Types.INTEGER,zongfushoushu, ParameterType.Input);
    //
    //		if(SqlHelperwrite.execute(cmdText, p1,p2,p3,p8,p9,p10,p11,p12,p13,p19,p22,p23,
    //				p24,p25,p28,p29,p30,p31,p32,p38,p39,p40,p41,p42,p61,pid)>0){
    //			preservationProp(Id);
    if (SqlHelperwrite.execute(cmdText, p1, p2, p3, pid) > 0) {
      return true;
    } else {
      return false;
    }


  }


  //修改用户在线状态
  public void updateOnline(boolean bool) {

    String cmdText = "UPDATE  users set online=? WHERE id=?";
    Parameter p1 = new Parameter(Types.BIT, bool, ParameterType.Input);
    Parameter p2 = new Parameter(Types.INTEGER, this.Id, ParameterType.Input);
    SqlHelperwrite.execute(cmdText, p1, p2);
  }


  /**
   * json 转 map
   *
   * @param json HashMap<String, Integer>
   */
  public static HashMap<String, Integer> jsonturnmap(String json) {
    Map mapTypes = JSON.parseObject(json);
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    for (Object obj : mapTypes.keySet()) {
      map.put((String) obj, (Integer) mapTypes.get(obj));
    }
    return map;
  }

  public static int[][] checkMianFei(String str) {
    int[][] mianfei = new int[4][2];
    String[] yi = str.split("\\*");
    if (yi.length == 4) {
      boolean bool = false;
      for (int i = 0; i < 4; i++) {
        String str2 = yi[i];
        String[] er = str2.split(",");
        if (er.length == 2) {
          for (int j = 0; j < 2; j++) {
            mianfei[i][j] = Integer.parseInt(er[j]);
          }
        } else {
          bool = true;
          break;
        }

      }
      if (bool) {
        LogManager.getInstance().Log("数据有问题 重置");
        for (int i = 0; i < 4; i++) {
          for (int j = 0; j < 2; j++) {
            mianfei[i][j] = 0;
          }
        }
      }
    } else {
      LogManager.getInstance().Log("数据有问题 重置");
      for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 2; j++) {
          mianfei[i][j] = 0;
        }
      }

    }

    return mianfei;

  }

  /**
   * 拼接免费道具使用
   */
  public String pingjieMiefei(int[][] mianfei) {
    String str = "";
    if (mianfei.length == 4) {
      for (int i = 0; i < 4; i++) {

        for (int j = 0; j < 2; j++) {
          str += String.valueOf(mianfei[i][j]);
          if (j == 0) {
            str += ",";
          }
        }
        if (i < 3) {
          str += "*";
        }
      }
    } else {
      str = "0,0*0,0*0,0*0,0";
    }

    return str;

  }


  public void updateUserCreateposition(String str) {
    String sql = "UPDATE  users SET createposition = ? WHERE id = ?";
    Parameter p1 = new Parameter(Types.VARCHAR, str, ParameterType.Input);
    Parameter p2 = new Parameter(Types.INTEGER, Id, ParameterType.Input);
    SqlHelperwrite.execute(sql, p1, p2);

  }

  public String checklovepai(String lovepai) {
    String newlove = "";
    if (lovepai != null && !"".equals(lovepai)) {
      String[] contents = lovepai.split(";");
      for (int i = 0; i < contents.length; i++) {
        if (!contents[i].isEmpty() && !contents[i].equals("null")) {
          newlove = newlove + ";" + contents[i];
        }
      }
    }
    return newlove;
  }

} 
